<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2006/05/26 17:32:08 $)
// $RCSfile: functions_tasks.php,v $ : $Revision: 1.4 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

/**
* Insert a new Calendar Task
*/
function insertCalendarTask($subject, $tasktype, $ownerstaffid, $duedate, $startdate, $contactid, $iscomplete, $completepercent, $calendarlabelid, $calendarstatusid, $calendarpriorityid, $notes)
{
	global $dbCore, $_SWIFT;

	$dbCore->query("INSERT INTO `". TABLE_PREFIX ."calendartasks` (`ownerstaffid`, `staffid`, `dateline`, `duedate`, `startdate`, `contactid`, `completepercent`, `iscomplete`, `tasktype`, `subject`, `calendarlabelid`, `calendarstatusid`, `calendarpriorityid`, `lastupdate`) VALUES ('". intval($ownerstaffid) ."', '". intval($_SWIFT["staff"]["staffid"]) ."', '". DATENOW ."', '". intval($duedate) ."', '". intval($startdate) ."', '". intval($contactid) ."', '". intval($completepercent) ."', '". intval($iscomplete) ."', '". $dbCore->escape($tasktype) ."', '". $dbCore->escape($subject) ."', '".intval($calendarlabelid)."', '". intval($calendarstatusid) ."', '". intval($calendarpriorityid) ."', '". DATENOW ."');");
	$calendartaskid = $dbCore->lastInsertId();
	if (!$calendartaskid)
	{
		return false;
	}

	$dbCore->query("INSERT INTO `". TABLE_PREFIX ."calendardata` (`datatype`, `typeid`, `contents`) VALUES ('". TEAMWORK_TASK ."', '". intval($calendartaskid) ."', '". $dbCore->escape($notes) ."');");

	return $calendartaskid;
}

/**
* Update Calendar Task
*/
function updateCalendarTask($calendartaskid, $subject, $tasktype, $ownerstaffid, $duedate, $startdate, $contactid, $iscomplete, $completepercent, $calendarlabelid, $calendarstatusid, $calendarpriorityid, $notes)
{
	global $dbCore, $_SWIFT;

	$dbCore->query("UPDATE `". TABLE_PREFIX ."calendartasks` SET `ownerstaffid` = '". intval($ownerstaffid) ."', `duedate` = '". intval($duedate) ."', `startdate` = '". intval($startdate) ."', `contactid` = '". intval($contactid) ."', `completepercent` = '". intval($completepercent) ."', `iscomplete` = '". intval($iscomplete) ."', `tasktype` = '". $dbCore->escape($tasktype) ."', `subject` = '". $dbCore->escape($subject) ."', `calendarlabelid` = '". intval($calendarlabelid) ."', `calendarstatusid` = '". intval($calendarstatusid) ."', `calendarpriorityid` = '". intval($calendarpriorityid) ."', `lastupdate` = '". DATENOW ."' WHERE `calendartaskid` = '". intval($calendartaskid) ."';");

	$dbCore->query("UPDATE `". TABLE_PREFIX ."calendardata` SET `contents` = '". $dbCore->escape($notes) ."' WHERE `datatype` = '". TEAMWORK_TASK ."' AND  `typeid` = '". intval($calendartaskid) ."';");

	return true;
}

/**
* Delete a Calendar Tasks
*/
function deleteCalendarTasks($calendartaskidlist, $guidid = 0)
{
	global $dbCore, $_SWIFT;

	if (!_is_array($calendartaskidlist))
	{
		return false;
	}

	$finaltaskidlist = array();
	$dbCore->query("SELECT `calendartaskid` FROM `". TABLE_PREFIX ."calendartasks` WHERE ((`tasktype` = '". SWIFTPUBLIC ."') OR (`tasktype` = '". SWIFTPRIVATE ."' AND `staffid` = '". intval($_SWIFT["staff"]["staffid"]) ."')) AND `calendartaskid` IN (". buildIN($calendartaskidlist) .");");
	while ($dbCore->nextRecord())
	{
		$finaltaskidlist[] = $dbCore->Record["calendartaskid"];
	}

	$dbCore->query("DELETE FROM `". TABLE_PREFIX ."calendartasks` WHERE `calendartaskid` IN(". buildIN($finaltaskidlist) .");");
	$dbCore->query("DELETE FROM `". TABLE_PREFIX ."calendardata` WHERE `datatype` = '". TEAMWORK_TASK ."' AND `typeid` IN(". buildIN($finaltaskidlist) .");");

	// First load the links
	$_insertsql = array();
	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."calendarlinks` WHERE `linktype` = '". LINK_TASK ."' AND `typeid` IN (". buildIN($finaltaskidlist) .");");
	while ($dbCore->nextRecord())
	{
		$_insertsql[] = "('". TEAMWORK_TASK ."', '". $dbCore->escape($dbCore->Record["entryid"]) ."', '". DATENOW ."', '". intval($guidid) ."')";
	}

	if (count($_insertsql))
	{
		$dbCore->query("INSERT INTO `". TABLE_PREFIX ."twdeletelog` (`entrytype`, `entryid`, `dateline`, `guidid`) VALUES ". implode(",", $_insertsql).";");
	}

	$dbCore->query("DELETE FROM `". TABLE_PREFIX ."calendarlinks` WHERE `linktype` = '". LINK_TASK ."' AND `typeid` IN (". buildIN($finaltaskidlist) .");");

	return true;
}

/**
* Marks the tasks as overdue
*/
function markTasksAsOverdue($calendartaskidlist)
{
	global $dbCore;

	if (!_is_array($calendartaskidlist))
	{
		return false;
	}

	$overduetimeline = DATENOW-1;
	$dbCore->query("UPDATE `". TABLE_PREFIX ."calendartasks` SET `duedate` = '". intval($overduetimeline) ."' WHERE `calendartaskid` IN (". buildIN($calendartaskidlist) .");");

	return true;
}

/**
* Marks the tasks as incomplete
*/
function markTasksAsInComplete($calendartaskidlist)
{
	global $dbCore;

	if (!_is_array($calendartaskidlist))
	{
		return false;
	}

	$dbCore->query("UPDATE `". TABLE_PREFIX ."calendartasks` SET `iscomplete` = '0' WHERE `calendartaskid` IN (". buildIN($calendartaskidlist) .");");

	return true;
}

/**
* Marks the tasks as Complete
*/
function markTasksAsComplete($calendartaskidlist)
{
	global $dbCore;

	if (!_is_array($calendartaskidlist))
	{
		return false;
	}

	$dbCore->query("UPDATE `". TABLE_PREFIX ."calendartasks` SET `iscomplete` = '1', `completepercent` = '100', `duedate` = '0' WHERE `calendartaskid` IN (". buildIN($calendartaskidlist) .");");

	return true;
}

?>